package p270jp.pxv.android.viewholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.common.api.Api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Objects;
import p014ae.View$OnClickListenerC0050b;
import p014ae.View$OnClickListenerC0156s7;
import p045be.C1060c;
import p045be.View$OnClickListenerC1058b0;
import p134ei.AnalyticsAreaName;
import p134ei.AnalyticsScreenName;
import p134ei.PixivAnalytics;
import p165fo.C3114c;
import p184gi.ClickEvent;
import p210hh.PixivAccountManager;
import p270jp.pxv.android.R;
import p270jp.pxv.android.activity.FollowUserActivity;
import p270jp.pxv.android.activity.MyPixivUsersActivity;
import p270jp.pxv.android.event.OpenUrlEvent;
import p270jp.pxv.android.legacy.model.PixivUser;
import p270jp.pxv.android.model.PixivProfile;
import p270jp.pxv.android.model.PixivProfilePublicity;
import p270jp.pxv.android.model.PixivWorkspace;
import p307l9.EdgeTreatment;
import p314lg.ViewProfileWorkspaceItemBinding;
import p314lg.ViewUserProfileInfoBinding;
import p350mp.Timber;
import p374nm.View$OnClickListenerC5388c0;

/* renamed from: jp.pxv.android.viewholder.UserProfileViewHolder */
/* loaded from: classes2.dex */
public class UserProfileViewHolder extends RecyclerView.AbstractC0819y {
    private final ViewUserProfileInfoBinding binding;
    private final int defaultCaptionLines;
    private final PixivAnalytics pixivAnalytics;
    private PixivProfile profile;
    private PixivUser user;
    private WorkspaceAdapter workSpaceAdapter;

    /* renamed from: jp.pxv.android.viewholder.UserProfileViewHolder$WorkspaceAdapter */
    /* loaded from: classes2.dex */
    public class WorkspaceAdapter extends BaseAdapter {
        private String[] keys;
        private LinkedHashMap<String, String> workspaceMap;

        public WorkspaceAdapter(Context context, PixivWorkspace pixivWorkspace) {
            UserProfileViewHolder.this = r1;
            LinkedHashMap<String, String> createWorkspaceMap = createWorkspaceMap(context, pixivWorkspace);
            this.workspaceMap = createWorkspaceMap;
            this.keys = (String[]) createWorkspaceMap.keySet().toArray(new String[this.workspaceMap.size()]);
        }

        private LinkedHashMap<String, String> createWorkspaceMap(Context context, PixivWorkspace pixivWorkspace) {
            LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
            if (!TextUtils.isEmpty(pixivWorkspace.f19134pc)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_computer), pixivWorkspace.f19134pc);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.monitor)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_monitor), pixivWorkspace.monitor);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.tool)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_software), pixivWorkspace.tool);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.scanner)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_scanner), pixivWorkspace.scanner);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.tablet)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_tablet), pixivWorkspace.tablet);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.mouse)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_mouse), pixivWorkspace.mouse);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.printer)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_printer), pixivWorkspace.printer);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.desktop)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_on_table), pixivWorkspace.desktop);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.music)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_music), pixivWorkspace.music);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.desk)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_table), pixivWorkspace.desk);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.chair)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_chair), pixivWorkspace.chair);
            }
            if (!TextUtils.isEmpty(pixivWorkspace.comment)) {
                linkedHashMap.put(context.getString(R.string.user_workspace_other), pixivWorkspace.comment);
            }
            return linkedHashMap;
        }

        @Override // android.widget.Adapter
        public int getCount() {
            return this.workspaceMap.size();
        }

        @Override // android.widget.Adapter
        public Object getItem(int i) {
            return this.workspaceMap.get(this.keys[i]);
        }

        @Override // android.widget.Adapter
        public long getItemId(int i) {
            return (long) i;
        }

        @Override // android.widget.Adapter
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view2;
            ViewProfileWorkspaceItemBinding zaVar;
            if (view == null) {
                zaVar = (ViewProfileWorkspaceItemBinding) C1060c.m10358a(viewGroup, R.layout.view_profile_workspace_item, viewGroup, false);
                view2 = zaVar.f2666e;
                view2.setTag(zaVar);
            } else {
                zaVar = (ViewProfileWorkspaceItemBinding) view.getTag();
                view2 = view;
            }
            String str = this.keys[i];
            zaVar.f22554r.setText(str);
            zaVar.f22553q.setText(this.workspaceMap.get(str));
            zaVar.m11392i();
            return view2;
        }

        @Override // android.widget.BaseAdapter, android.widget.ListAdapter
        public boolean isEnabled(int i) {
            return false;
        }
    }

    private UserProfileViewHolder(ViewUserProfileInfoBinding ecVar, int i, PixivAnalytics eVar) {
        super(ecVar.f2666e);
        this.binding = ecVar;
        this.defaultCaptionLines = i;
        this.pixivAnalytics = eVar;
    }

    public static UserProfileViewHolder createViewHolderByParentView(ViewGroup viewGroup, PixivAnalytics eVar) {
        return new UserProfileViewHolder((ViewUserProfileInfoBinding) C1060c.m10358a(viewGroup, R.layout.view_user_profile_info, viewGroup, false), viewGroup.getContext().getResources().getInteger(R.integer.default_caption_lines), eVar);
    }

    /* renamed from: d */
    public static /* synthetic */ void m4382d(Context context, PixivUser pixivUser, View view) {
        lambda$onBindViewHolder$3(context, pixivUser, view);
    }

    private String formatAgeFromBirthday(String str) {
        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse(str);
            Date date = new Date();
            Calendar instance = Calendar.getInstance();
            instance.setTime(parse);
            Calendar instance2 = Calendar.getInstance();
            instance2.setTime(date);
            int i = instance2.get(1) - instance.get(1);
            if (instance.get(2) > instance2.get(2) || (instance.get(2) == instance2.get(2) && instance.get(5) > instance2.get(5))) {
                i--;
            }
            return this.itemView.getContext().getString(R.string.user_age_format, Integer.valueOf(i));
        } catch (ParseException e) {
            Timber.f23100a.mo3523f(e, "parse error", new Object[0]);
            return null;
        }
    }

    private String formatBirthday(String str) {
        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse(str);
            Calendar instance = Calendar.getInstance();
            instance.setTime(parse);
            return this.itemView.getContext().getString(R.string.user_birth, Integer.valueOf(instance.get(2) + 1), Integer.valueOf(instance.get(5)));
        } catch (ParseException e) {
            Timber.f23100a.mo3523f(e, "birth", new Object[0]);
            return null;
        }
    }

    private String formatGender(String str) {
        Objects.requireNonNull(str);
        if (str.equals(PixivProfile.GENDER_FEMALE)) {
            return this.itemView.getContext().getString(R.string.female);
        }
        if (!str.equals(PixivProfile.GENDER_MALE)) {
            return "unknown";
        }
        return this.itemView.getContext().getString(R.string.male);
    }

    public /* synthetic */ void lambda$onBindViewHolder$0(PixivUser pixivUser, PixivProfile pixivProfile, View view) {
        this.pixivAnalytics.m6526d(new ClickEvent(17, null, null, null, AnalyticsScreenName.USER_PROFILE, Long.valueOf(pixivUser.f19078id), AnalyticsAreaName.USER_PROFILE_INFO, null, null));
        C3114c.m6284b().m6280f(new OpenUrlEvent(pixivProfile.webpage));
    }

    public /* synthetic */ void lambda$onBindViewHolder$1(PixivUser pixivUser, PixivProfile pixivProfile, View view) {
        this.pixivAnalytics.m6526d(new ClickEvent(18, null, null, null, AnalyticsScreenName.USER_PROFILE, Long.valueOf(pixivUser.f19078id), AnalyticsAreaName.USER_PROFILE_INFO, null, null));
        C3114c.m6284b().m6280f(new OpenUrlEvent(pixivProfile.twitterUrl));
    }

    public static /* synthetic */ void lambda$onBindViewHolder$2(Context context, PixivUser pixivUser, View view) {
        context.startActivity(FollowUserActivity.m4827e1(context, pixivUser.f19078id));
    }

    public static /* synthetic */ void lambda$onBindViewHolder$3(Context context, PixivUser pixivUser, View view) {
        context.startActivity(MyPixivUsersActivity.m4810e1(context, pixivUser.f19078id));
    }

    private void setUserProfile(PixivProfile pixivProfile) {
        ArrayList arrayList = new ArrayList();
        String formatGender = formatGender(pixivProfile.gender);
        if (!formatGender.equals("unknown")) {
            arrayList.add(formatGender);
        }
        if (!TextUtils.isEmpty(pixivProfile.region)) {
            arrayList.add(pixivProfile.region);
        }
        if (!TextUtils.isEmpty(pixivProfile.birth) && !pixivProfile.birth.equals("0000-00-00")) {
            arrayList.add(formatAgeFromBirthday(pixivProfile.birth));
            arrayList.add(formatBirthday(pixivProfile.birth));
        }
        if (!TextUtils.isEmpty(pixivProfile.job)) {
            arrayList.add(pixivProfile.job);
        }
        this.binding.f21542w.setText(TextUtils.join("/", arrayList.toArray()));
    }

    public void onBindViewHolder(PixivUser pixivUser, PixivProfile pixivProfile, PixivWorkspace pixivWorkspace, PixivProfilePublicity pixivProfilePublicity) {
        EdgeTreatment.m3909i(pixivUser);
        EdgeTreatment.m3909i(pixivProfile);
        EdgeTreatment.m3909i(pixivProfilePublicity);
        this.user = pixivUser;
        this.profile = pixivProfile;
        this.binding.f21533B.setText(pixivUser.name);
        if (pixivProfile.isPremium) {
            this.binding.f21541v.setVisibility(0);
        } else {
            this.binding.f21541v.setVisibility(8);
        }
        if (TextUtils.isEmpty(pixivProfile.webpage)) {
            this.binding.f21538s.setVisibility(8);
        } else {
            this.binding.f21538s.setText(pixivProfile.webpage.replaceFirst("^(http|https):\\/\\/", ""));
            this.binding.f21538s.setOnClickListener(new View$OnClickListenerC5388c0(this, pixivUser, pixivProfile, 1));
        }
        if (TextUtils.isEmpty(pixivProfile.twitterAccount)) {
            this.binding.f21532A.setVisibility(8);
        } else {
            this.binding.f21532A.setText(pixivProfile.twitterAccount);
            this.binding.f21532A.setOnClickListener(new View.OnClickListener(pixivUser, pixivProfile) { // from class: jp.pxv.android.viewholder.o

                /* renamed from: b */
                public final /* synthetic */ PixivUser f19780b;

                /* renamed from: c */
                public final /* synthetic */ PixivProfile f19781c;

                {
                    this.f19780b = r2;
                    this.f19781c = r3;
                }

                @Override // android.view.View.OnClickListener
                public final void onClick(View view) {
                    UserProfileViewHolder.this.lambda$onBindViewHolder$1(this.f19780b, this.f19781c, view);
                }
            });
        }
        this.binding.f21537r.setText(String.valueOf(pixivProfile.totalFollowUsers));
        this.binding.f21540u.setText(String.valueOf(pixivProfile.totalMypixivUsers));
        setUserProfile(pixivProfile);
        Context context = this.itemView.getContext();
        WorkspaceAdapter workspaceAdapter = new WorkspaceAdapter(context, pixivWorkspace);
        this.workSpaceAdapter = workspaceAdapter;
        if (workspaceAdapter.getCount() > 0) {
            this.binding.f21535D.setAdapter((ListAdapter) this.workSpaceAdapter);
        } else {
            this.binding.f21534C.setVisibility(8);
        }
        if (TextUtils.isEmpty(pixivUser.comment)) {
            this.binding.f21545z.setVisibility(8);
            if (this.workSpaceAdapter.getCount() == 0) {
                this.binding.f21544y.setVisibility(8);
            }
            showProfileTextView();
        } else {
            this.binding.f21545z.setText(pixivUser.comment);
        }
        this.binding.f21545z.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { // from class: jp.pxv.android.viewholder.UserProfileViewHolder.1
            @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
            public void onGlobalLayout() {
                UserProfileViewHolder.this.binding.f21545z.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                if (TextUtils.isEmpty(UserProfileViewHolder.this.binding.f21542w.getText()) && UserProfileViewHolder.this.workSpaceAdapter.getCount() == 0 && UserProfileViewHolder.this.binding.f21545z.getLineCount() < UserProfileViewHolder.this.defaultCaptionLines) {
                    UserProfileViewHolder.this.binding.f21544y.setVisibility(8);
                }
            }
        });
        this.binding.f21536q.setOnClickListener(new View$OnClickListenerC1058b0(context, pixivUser, 6));
        if (pixivProfile.totalMypixivUsers == 0) {
            this.binding.f21539t.setVisibility(8);
        } else {
            this.binding.f21539t.setOnClickListener(new View$OnClickListenerC0156s7(context, pixivUser, 10));
        }
        this.binding.f21544y.setOnClickListener(new View$OnClickListenerC0050b(this, 27));
    }

    public void onReadMoreSelfIntroduceClick(View view) {
        this.binding.f21544y.setVisibility(8);
        this.binding.f21545z.setMaxLines(Api.BaseClientBuilder.API_PRIORITY_OTHER);
        if (this.workSpaceAdapter.getCount() > 0) {
            this.binding.f21534C.setVisibility(0);
        }
        showProfileTextView();
    }

    public void showProfileTextView() {
        if (!TextUtils.isEmpty(this.binding.f21542w.getText())) {
            if (PixivAccountManager.m5511e().f16499e == this.user.f19078id) {
                this.binding.f21543x.setVisibility(0);
            }
            this.binding.f21542w.setVisibility(0);
        }
    }
}
